package org.fyp.synchrona.moodle.service;

import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.fyp.synchrona.core.info.impl.ICommonDataObject;
import org.fyp.synchrona.moodle.service.util.NewData;
import org.fyp.synchrona.moodle.service.util.OldData;
import org.fyp.synchrona.moodle.service.util.Update;
import org.fyp.synchrona.service.IService;
import org.fyp.synchrona.util.IAccount;
import org.fyp.synchrona.util.IUpdate;


public class MoodleService implements IService {

	private String serviceName = "Moodle";

	public String userName;

	public String password;

	static final Logger logger = Logger.getLogger(MoodleService.class);
	
	@Override
	public void executeTask() {

		
	}

	@Override
	public void startService(IUpdate updateObj) {
		logger.info("[MOODLE] ###########################Start Information from Moodle Service Plugin########################");
		getuserNamePassword(updateObj.getAccount());
		Updater updater = new Updater(userName, password);
		updater.startUpdate(updateObj);
		logger.info("[MOODLE] ###########################End Information from Moodle Service Plugin########################");
	}
	
	private void getuserNamePassword(List<IAccount> account) {

		IAccount element= null;
		Iterator i = account.listIterator();

		logger.info("[MOODLE] _+_+_+_+_+_+_+_+_+_+_+_+_+update data_+_+_+_+_+_+_+_+_+_+_+_+_+_");
		while(i.hasNext()) {
			element =  (IAccount) i.next(); 
			logger.info("[MOODLE] ++++++++++++++++++++++++service name+++++++++++++++++++++++++++");
		    logger.info("[MOODLE] servicename"+element.getServiceName());
			if(element.getServiceName().equals(serviceName)){
				this.userName = element.getUserName();
				this.password = element.getPassword();
				break;
			}
			logger.info("[MOODLE] ++++++++++++++++++++++++user name+++++++++++++++++++++++++++");
			logger.info("[MOODLE] username"+element.getUserName());
			logger.info("[MOODLE] ++++++++++++++++++++++++password+++++++++++++++++++++++++++");
			logger.info("[MOODLE] password"+element.getUserName());
			
		} 
		
	}
	
	public static void main(String arg[]){
		
		IUpdate updateObj = new Update();
		ICommonDataObject cdo = new CommonDataObject();
		cdo.setEMAIL("a@b.com");
		cdo.setUSER_NAME("admin123");
		cdo.setFIRST_NAME("firstname");
		cdo.setLAST_NAME("lastname");
		cdo.setCITY("Colombo");
		cdo.setBIO("bio testsdfsdfsdfsdf");
		cdo.setINTERESTS("interesttest");
		updateObj.setCdo(cdo);
		updateObj.setUpdateType("PROFILE");
		new MoodleService().startService(updateObj);
		
	}
	
	

}
